﻿var myshow;
var carousel;
var firstR = false;

function getScrollXY() {
      var scrOfX = 0, scrOfY = 0;
      if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
      } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
      }
      return [ scrOfX, scrOfY ];
};
//Fonction pour installer le slider
function setupSlider(){
    var parent = document.getElementById('carousel-content-slide');
    var el = document.getElementById('slide-1');
    var html = '<li class="slide">'+el.innerHTML+'</li>';
    parent.insert(html);
    
    carousel = new Carousel('carousel-wrapper-slide', $$('#carousel-content-slide .slide'), $$('a.carousel-jumper'),{frequency:3,auto:false,circular:false,wheel: false, afterMove: function(element){
                                                                                                                                                                                        
        $$('.control-nav a').invoke('removeClassName', 'carousel-selected');
        
        if(this.current == null){
            $$('.control-nav a')[0].addClassName('carousel-selected');
        }else{
            $$('.control-nav a')[this.current - 1].addClassName('carousel-selected');
        }
    }});
    
    $$('.control-nav a')[0].addClassName('carousel-selected');
    
    
};

function resizePage(){
    if(!isMobile()){
       
        var viewport = document.viewport.getDimensions(); 
        var vh = viewport.height;
        var h  = $$('#carousel-content-slide li.slide img')[0].getHeight();
        var compteReboursHeight = $('compte_rebours').getHeight();
        var block_bottom_height = 46;

        //var topcompteRebours = ((h - compteReboursHeight)/2)-(compteReboursHeight/2); // Calculer le top du compte a rebours
        //if(topcompteRebours < 0)
        {
          var topcompteRebours = (((h-150) - compteReboursHeight)/2);
        }
        if(h < (vh - block_bottom_height)){
            
            $('carousel-wrapper-slide').setStyle({
                height : h+'px'
            });
            $$('.slide').each(function(item){
                item.setStyle({
                    height : h+'px'
                })
            });
            
        }else{
            $('carousel-wrapper-slide').setStyle({
                height : (vh-block_bottom_height)+'px'
            });
            $$('.slide').each(function(item){
                item.setStyle({
                    height : (vh-block_bottom_height)+'px'
                })
            });
        }
        
        //Centrer verticalement le compte a robot
        
        $('compte_rebours').setStyle({
            'top' : topcompteRebours+'px'
        });
    
     

        firstR = true;
    }
    
    //Centrer verticalement
    var sliderHeight         = $('carousel-wrapper-slide').getDimensions().height; // le height du slider
    var controlNavHeight     = $('control-nav').getDimensions().height; // le height du control-nav 
    var topControlNav        = (sliderHeight - controlNavHeight)/2; // Calculer le top du control-nav

};

function isMobile(){
    if( navigator.userAgent.match(/Android/i)
         || navigator.userAgent.match(/webOS/i)
         || navigator.userAgent.match(/iPhone/i)
        || navigator.userAgent.match(/iPad/i)
         || navigator.userAgent.match(/iPod/i)
         || navigator.userAgent.match(/BlackBerry/i)
         || navigator.userAgent.match(/Windows Phone/i)
     ){
        return true;
      }
     else {
        return false;
      }
};
function autoScroll()
{
    var scrollXY = getScrollXY();
    var window_scrollX = scrollXY[0];
    var window_scrollTop = scrollXY[1];
    var bottom_box_margin_top = 156;
    var form_mail_height = 0;
    if ($('bottom_box').hasClassName('bottom_box_fixed')) 
    {
            if (window_scrollTop < ($('carousel-wrapper-slide').getHeight()- (bottom_box_margin_top+form_mail_height))) {
                $('bottom_box').removeClassName('bottom_box_fixed');
                $$('#form_mail_carousel .join_show')[0].show();
                $('detail_box').setStyle({'top':'93%'});
            }
    }
    else 
    {
        if (window_scrollTop > ($('carousel-wrapper-slide').getHeight()-(bottom_box_margin_top+form_mail_height))) {
            $('bottom_box').addClassName('bottom_box_fixed');
            $$('#form_mail_carousel .join_show')[0].hide();
            $('detail_box').setStyle({'top':'89%'});
        }
    }
};
Event.observe(window, "load", function() {

   setupSlider();
   t = setTimeout(function(){
           // carousel.resume();
        },2000
    );
   resizePage();
   autoScroll();
    //si on click sur d'oeil
    $('effects_bottom_box').observe("click",function(event, element) {
        var this_object = this;
        //changer l'image d'oeil
        if(this_object.hasClassName('show_box'))
            {
            this_object.removeClassName('show_box');
            this_object.addClassName('hide_box');
            }
        else
            {
            this_object.removeClassName('hide_box');
            this_object.addClassName('show_box');
            }
        var bottom_box = $('bottom_box');
        
        if(bottom_box.hasClassName('bottom_box'))
            {
              bottom_box.removeClassName('bottom_box');
              bottom_box.addClassName('bottom_box_hide');
              
            }
        else
            {
              bottom_box.removeClassName('bottom_box_hide');
              bottom_box.addClassName('bottom_box');
            }
        //show hide compte rebours
        var compte_rebours = $('compte_rebours');
        if(compte_rebours.hasClassName('compte_rebours'))
        {
            compte_rebours.removeClassName('compte_rebours');
            compte_rebours.addClassName('compte_rebours_hide');
          
        }
    else
        {
        compte_rebours.removeClassName('compte_rebours_hide');
        compte_rebours.addClassName('compte_rebours');
        }
        

     });
    //play video on click over scroll
   $('carousel-content').select('a.play_video').each(function(e,index)
    {
        var ifream_video =  $('youtube_video');
        $(e).observe("click",function(event, element) {
           event.stop();
            //show  video selected content
            $$('.bloc_content_box').invoke('removeClassName', 'current_box_selected');
            
            var index_select = index;
            
            if(typeof $$('.bloc_content_box')[index_select] == "undefined")
            {
                index_select = 0;
            }
            
            $$('.bloc_content_box')[index_select].addClassName('current_box_selected');
          
           //select the current elements
           var parent_li_node =  $($(this).parentNode.parentNode);
           parent_li_node.addClassName('selected');
           //deselect others elements 
           $('carousel-content').select('li').each(function(el,index_2)
            {
               if($(el)!= parent_li_node)
                {
                  $(el).removeClassName('selected');
                }
               
             });
         //show  video
           if(!$('bottom_box').hasClassName('bottom_box_fixed'))
             {
                 //hied the slideshow content
                  Effect.ScrollTo('form_mail_carousel', { duration:'0.3'}); // works in v1.8.2
                 //hide form email
                 //stop scroller
                 carousel.stop();
            }

           //play video in ifream
            var url_video = $(this).readAttribute('href');
            if(url_video != null && url_video != '#' )
             {
               $('youtube_video').writeAttribute("src", "" );
               $('youtube_video').writeAttribute("src", url_video );
             }
            
           return false;
         });
    });
   //close video content
   $('close_video_iframe').observe("click",function(event, element) {
       event.stop();
       //show form email
       //show the slideshow content
       Effect.ScrollTo('carousel-wrapper-slide', { duration:'0.3'}); // works in v1.8.2
       $('bottom_box').removeClassName('bottom_box_intop');
       //stop video in ifream
       var old_src_video =$('youtube_video').readAttribute("src");
       $('youtube_video').writeAttribute("src", old_src_video );
   });
   //open lang list
   $('ul_language_selected').select('li.language_selected').each(function(e,index)
   {
       $(e).observe("click",function(event, element) {
           event.stop();
           var this_object = $(this);
           var language_select = $('language_select_content');
           if(this_object.hasClassName('closed'))
            {
               this_object.removeClassName('closed');
               this_object.addClassName('open');
               language_select.appear();
            }
           else
            {
               this_object.removeClassName('open');
               this_object.addClassName('closed');
               language_select.fade();
             }
           
       });
   });
   //select lang
   $('language_select').select('li.language').each(function(e,index)
    {
       var language_select_content = $('language_select_content');
       var language_selected_list = $('ul_language_selected').down('li.language_selected');
       var language_selected = $('ul_language_selected').down('li.language_selected a');
       $(e).observe("click",function(event, element) {
           event.stop();
           var this_object = $(this);
           var language_select = $('language_select').down('li.selected');
           language_select.removeClassName('selected');
           this_object.addClassName('selected');
           language_selected.update($(this).down('a').text);
           var url_lang = this_object.down('a').readAttribute('href');
           language_select_content.fade();
           //close liste lang
           language_selected_list.removeClassName('open');
           language_selected_list.addClassName('closed');
           
           if(url_lang != null && url_lang != '#' )
            {
              document.location.href=url_lang;
            }
       });
    });
   //
    //Carousel
   var nb_slides = $$('#carousel-content .slide_li').length;
   if(nb_slides <=5)
     {
         $('c_next').hide();
         $('c_prev').hide();
     }
   nb_slides_total = nb_slides;
   var re_carousel = new Carousel('carousel-wrapper', $$('#carousel-content .slide_li'), $$('a.carousel-control'),{visibleSlides:5,afterMove: function(element){
       nb_slides_total+=5;
       nextIndex = re_carousel.current._index;
        if (re_carousel.current._index > re_carousel.slides.length - (re_carousel.options.visibleSlides + 1)) {
            nextIndex = re_carousel.slides.length - re_carousel.options.visibleSlides;
        }        

       if(nextIndex == 0){
           $('c_prev').hide();
       }
       else
           {
           $('c_prev').show();
           }
       if(nextIndex > (re_carousel.slides.length - (re_carousel.options.visibleSlides + 1)))
       {
           $('c_next').hide();
       }
       else
           {
           $('c_next').show();
           }
   }});
   
    Event.observe(window, "resize", function() {
        resizePage();
    });
    
    Event.observe(window, "scroll",function() {
        autoScroll();
    });
    
});
